package com.sunyur.tailor.channel.web.outer.supplier.controller;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.common.middle.util.BeanCopyUtil;
import com.sunyur.tailor.channel.service.inner.supplier.param.SupplierBankParam;
import com.sunyur.tailor.channel.service.inner.supplier.param.SupplierParam;
import com.sunyur.tailor.channel.service.inner.supplier.param.SupplierUserParam;
import com.sunyur.tailor.channel.service.inner.supplier.service.SunyurSupplierService;
import com.sunyur.tailor.channel.web.outer.supplier.dto.SupplierBankDTO;
import com.sunyur.tailor.channel.web.outer.supplier.dto.SupplierDTO;
import com.sunyur.tailor.channel.web.outer.supplier.dto.SupplierUserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author xuhui
 * @Description 供应商控制层
 * @Date 15:45 2021/6/1
 * @Param
 * @return
 **/
@Slf4j
@RestController
@RequestMapping("/outer/supplier")
public class SupplierController {

    @Autowired
    private SunyurSupplierService sunyurSupplierService;

    /**
     * 批量创建更新供应商
     */
    @PostMapping(value = "/syncSupplierList", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult syncSupplierList(@RequestBody @Valid List<SupplierDTO> supplierDTOList) {
        log.info("SupplierController syncSupplierList param:{}", JSON.toJSONString(supplierDTOList));
        ChannelResult<Boolean> result = new ChannelResult<>();
        try {
            //数据转换
            List<SupplierParam> supplierParamList = BeanCopyUtil.copyList(supplierDTOList, SupplierParam.class);
            result = sunyurSupplierService.syncSupplierList(supplierParamList);
        } catch (Exception e) {
            log.error("SupplierController syncSupplierList exception:{}", e);
            result.setError(MiddleResultEnum.EXCEPTION.getCode(), MiddleResultEnum.EXCEPTION.getValue());
        }
        return result;
    }

    /**
     * 批量创建更新供应商联系人
     */
    @PostMapping(value = "/syncSupplierUserList", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult syncSupplierUserList(@RequestBody @Valid List<SupplierUserDTO> supplierUserDTOList) {
        log.info("SupplierController syncSupplierUserList param:{}", JSON.toJSONString(supplierUserDTOList));
        ChannelResult<Boolean> result = new ChannelResult<>();
        try {
            //数据转换
            List<SupplierUserParam> supplierUserParamList = BeanCopyUtil.copyList(supplierUserDTOList, SupplierUserParam.class);
            result = sunyurSupplierService.syncSupplierUserList(supplierUserParamList);
        } catch (Exception e) {
            log.error("SupplierController syncSupplierUserList exception:{}", e);
            result.setError(MiddleResultEnum.EXCEPTION.getCode(), MiddleResultEnum.EXCEPTION.getValue());
        }
        return result;
    }

    /**
     * 批量创建更新供应商银行信息
     */
    @PostMapping(value = "/syncSupplierBnakList", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult syncSupplierBnakList(@RequestBody @Valid List<SupplierBankDTO> supplierBankDTOList) {
        log.info("SupplierController syncSupplierBnakList param:{}", JSON.toJSONString(supplierBankDTOList));
        ChannelResult<Boolean> result = new ChannelResult<>();
        try {
            //数据转换
            List<SupplierBankParam> supplierBankParamList = BeanCopyUtil.copyList(supplierBankDTOList, SupplierBankParam.class);
            result = sunyurSupplierService.syncSupplierBnakList(supplierBankParamList);
        } catch (Exception e) {
            log.error("SupplierController syncSupplierBnakList exception:{}", e);
            result.setError(MiddleResultEnum.EXCEPTION.getCode(), MiddleResultEnum.EXCEPTION.getValue());
        }
        return result;
    }

}